/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Util.HibernateUtil;
import java.util.ArrayList;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.hibernate.Session;

/**
 *
 * @author aluno
 */
public class Receita_CRUD {

    public static void insertReceita(HttpServletRequest request) {
        Session hSession = HibernateUtil.getSessionFactory().getCurrentSession();
        Receita receita;
        Integer id = 0;
        String aux = request.getParameter("id");
        String login_user = (String) request.getParameter("user");
        if (!aux.isEmpty()) {
            id = Integer.valueOf(aux);
        }

        hSession.beginTransaction();

        receita = (Receita) hSession.createQuery("from Receita as "
                + "r where r.id =:id").setParameter("id", id).uniqueResult();
        Usuario user = (Usuario) hSession.createQuery("from Usuario as "
                + "c where c.login =:login").setParameter("login", login_user).uniqueResult();
        if (receita == null) {
            receita = new Receita();
            receita.setUsuario(user);
        }

        String name = (String) request.getParameter("name");
        String ingredientes = (String) request.getParameter("ingredientes");
        String clue = (String) request.getParameter("clue");
        Float time = Float.parseFloat(request.getParameter("time"));
        Integer avg = Integer.parseInt(request.getParameter("avg"));
        Integer val_nutri = Integer.valueOf(request.getParameter("val-nutri"));
        String categoria = (String) request.getParameter("categoria");
        String status = (String) request.getParameter("status");

        receita.setName(name);
        receita.setIngredientes(ingredientes);
        receita.setClue(clue);
        receita.setTime(time);
        receita.setVal_nutri(val_nutri);
        receita.setCategoria(categoria);
        receita.setStatus(status);

        if (receita.getId() != 0) {
            hSession.update(receita);
        } else {
            hSession.save(receita);
        }
        hSession.getTransaction().commit();

    }

    public static ArrayList getmyrecs(HttpServletRequest request, String active, Usuario user) {
        Session hSession = HibernateUtil.getSessionFactory().getCurrentSession();
        ArrayList<Receita> myrecs;
        hSession.beginTransaction();

        if (active.equals("yes")) {

            myrecs = (ArrayList) hSession.createQuery("SELECT r FROM Receita r, Usuario u "
                    + "where u = r.usuario AND u.id = :id AND r.status = 'yes' ").setParameter("id", user.getId()).list();
        } else {
            myrecs = (ArrayList) hSession.createQuery("SELECT r FROM Receita r, Usuario u "
                    + "where u = r.usuario AND u.id = :id").setParameter("id", user.getId()).list();
        }
        hSession.getTransaction().commit();

        return myrecs;
    }

    public static Receita detalharReceita(HttpServletRequest request, Integer id) {
        Session hSession = HibernateUtil.getSessionFactory().getCurrentSession();

        hSession.beginTransaction();
        Receita receita = (Receita) hSession.createQuery("from Receita as "
                + "r where r.id =:id").setParameter("id", id).uniqueResult();

        hSession.getTransaction().commit();

        return receita;

    }

    public static ArrayList comentariosReceita(Integer id) {
        Session hSession = HibernateUtil.getSessionFactory().getCurrentSession();
        
        hSession.beginTransaction();
        
        ArrayList<Comentario> comentario = (ArrayList) hSession.createQuery("SELECT c FROM Comentario c, Receita r, Usuario u" 
                     + " WHERE r = c.receita_id AND u = c.usuario_id"
                     + " AND r.id=:id").setParameter("id", id).list();
        
        hSession.getTransaction().commit();
        
        return comentario;
    }
    
    public static ArrayList likeReceita(Integer id) {
       Session hSession = HibernateUtil.getSessionFactory().getCurrentSession();
        
        hSession.beginTransaction(); 
        ArrayList<Receita> likeReceitas = (ArrayList<Receita>) hSession.createQuery("SELECT distinct r FROM Receita r, Nota_receita n, Usuario u"
                + " WHERE r = n.receita_id"
                + " AND u = n.usuario_id "
                + " AND u.id = :id "
                + " AND n.nota >= 3 GROUP BY r.id")
                .setParameter("id", id)
                .list();
        
        hSession.getTransaction().commit();
        
        return likeReceitas;
    }

    public static void listReceitaDisp(HttpServletRequest request, Usuario user) {
        HttpSession session = request.getSession();
        ArrayList<Receita> myrecs = Receita_CRUD.getmyrecs(request, "no", user);
        session.setAttribute("myrecs", myrecs);
        
        ArrayList<Receita> list_notes = Receita_CRUD.likeReceita(user.getId());
        session.setAttribute("like", list_notes);
    }

    public static void deletarReceita(HttpServletRequest request) {
        Integer rec = Integer.parseInt(request.getParameter("rec"));
        

        Session hSession = HibernateUtil.getSessionFactory().getCurrentSession();
        hSession.beginTransaction();
        Receita r = (Receita) hSession.get(Receita.class, rec);
        hSession.createQuery("DELETE Receita r WHERE r.id = :id ")
                .setParameter("id", r.getId())
                .executeUpdate();
        hSession.getTransaction().commit();
    }
    
    public static ArrayList listReceitas(){
        Session hSession = HibernateUtil.getSessionFactory().getCurrentSession();
        ArrayList<Receita> receitas;
        hSession.beginTransaction();

        receitas = (ArrayList) hSession.createQuery("FROM Receita").list();
       
        hSession.getTransaction().commit();

        return receitas;
    }

}
